package com.guacun.controller;

import com.guacun.entry.InfoManagerEntry;
import com.guacun.model.Student;
import com.guacun.service.StudentService;

import java.util.Scanner;

//学生管理系统
public class StudentController {
    private Scanner scanner = new Scanner(System.in);
    private StudentService studentService = new StudentService();

    //学生管理系统菜单
    public void studentMenu() {
        System.out.println("---------------------欢迎使用关村学生管理系统-----------------------");
        System.out.println("------1.添加学生    2.删除学生   3.修改学生  4.查询学生  5.back------");
        System.out.println("请输入您的选择:");
        while (true) {
            int i = scanner.nextInt();
            if (i == 1) {
                addinput();
            } else if (i == 2) {
                delete();
            } else if (i == 3) {
                update();
            } else if (i == 4) {
                selectall();
            } else if (i == 5) {
                System.out.println("退出成功");
                new InfoManagerEntry().mainMenu();
                break;
            } else {
                System.out.println("请重新输入您的选择:");
            }

        }
    }

    //老师管理系统菜单
    public void teacherMenu() {
        System.out.println("---------------------欢迎使用关村学生管理系统-----------------------");
        System.out.println("------1.添加学生    2.删除学生   3.修改学生  4.查询学生  5.back------");
        System.out.println("请输入您的选择:");
        while (true) {
            int i = scanner.nextInt();
            if (i == 1) {
                addinput();
            } else if (i == 2) {
                delete();
            } else if (i == 3) {
                update();
            } else if (i == 4) {
                selectall();
            } else if (i == 5) {
                System.out.println("退出成功");
                new InfoManagerEntry().mainMenu();
                break;
            } else {
                System.out.println("请重新输入您的选择:");
            }

        }
    }

    //添加
    public void addinput() {
        System.out.println("请输入学生id:");
        int id = scanner.nextInt();
        System.out.println("请输入学生姓名:");
        String name = scanner.next();
        System.out.println("请输入学生年龄:");
        int age = scanner.nextInt();
        System.out.println("请输入学生生日:");
        String birthday = scanner.next();
        int flag = studentService.addStudent(new Student(id, name, age, birthday));
        if (flag == 1) {
            studentMenu();
        } else {
            System.out.println("添加失败，请重新添加");
            addinput();
        }
    }

    //查询
    public void selectall() {
        int i = studentService.selectStudent();
        if (i == 1) {
            System.out.println("查询成功");
            studentMenu();
        } else {
            System.out.println("查询失败");
            studentMenu();
        }
    }

    //删除
    public void delete() {
        System.out.println("请输入学生id:");
        int id = scanner.nextInt();
        int i = studentService.deleteStudent(id);
        if (i == 1) {
            System.out.println("删除成功");
            studentMenu();
        } else {
            System.out.println("删除失败");
            delete();
        }
    }

    //修改
    public void update() {
        System.out.println("请输入真实学生id:");
        int id = scanner.nextInt();
        Student student = studentService.selectStudentbyid(id);
        if (null != student) {
            int k = studentService.updateStudent(student.getId());
            if (k == 1) {
                System.out.println("修改成功");
                studentMenu();
            }
        } else {
            System.out.println("id不存在!");
            update();
        }

    }
}
